package com.springboot.controller;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.text.SimpleDateFormat;

import com.springboot.dao.FurnitureMapper;

import com.springboot.entity.FurnitureExample;

import com.springboot.dao.AnnounceMapper;

import com.springboot.entity.AnnounceExample;

import java.util.Calendar;

import java.util.Collections;

import java.util.Date;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import java.util.Map;

import java.util.HashMap;

import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.multipart.MultipartFile;

@Controller

@RequestMapping(value = "/")

public class IndexController {

    private static final Log logger = LogFactory.getLog(IndexController.class);

    @Autowired
    private FurnitureMapper furnituredao;

    @Autowired
    private AnnounceMapper announcedao;

    @RequestMapping(value = "index")

    public String index(HttpServletRequest request, HttpServletResponse response, HttpSession session) {

        logger.debug("IndexController.index ......");// 实例化FurnitureExample
        FurnitureExample example1 = new FurnitureExample();

// 实例化FurnitureExample内部类Criteria  
        FurnitureExample.Criteria criteria1 = example1.createCriteria();

// 查询所有家具保存到furnitureall1中  
        List furnitureall1 = furnituredao.selectByExample(example1);

// 定义最新家具list  
        List furniturezuixin5 = null;

// 如果所有的家具数量大于了 5 
        if (furnitureall1.size() > 5) {

// 截取furnitureall1中的前5位保存到最新家具list中 
            furniturezuixin5 = furnitureall1.subList(furnitureall1.size() - 6, furnitureall1.size() - 1);

        } else {

// 将所有家具添加到最新家具list中 
            furniturezuixin5 = furnitureall1;

        }

// 将最新家具list保存到request中 
        request.setAttribute("furniturezuixin5", furniturezuixin5);

// 实例化AnnounceExample  
        AnnounceExample example2 = new AnnounceExample();

// 实例化AnnounceExample内部类Criteria  
        AnnounceExample.Criteria criteria2 = example2.createCriteria();

// 查询所有公告保存到announceall2中  
        List announceall2 = announcedao.selectByExample(example2);

// 定义最新公告list  
        List announcezuixin5 = null;

// 如果所有的公告数量大于了 5 
        if (announceall2.size() > 5) {

// 截取announceall2中的前5位保存到最新公告list中 
            announcezuixin5 = announceall2.subList(announceall2.size() - 6, announceall2.size() - 1);

        } else {

// 将所有公告添加到最新公告list中 
            announcezuixin5 = announceall2;

        }

// 将最新公告list保存到request中 
        request.setAttribute("announcezuixin5", announcezuixin5);

        return "index";

    }


    @ResponseBody
    @RequestMapping(value = "indexjson")

    public Map indexjson(HttpServletRequest request, HttpServletResponse response, HttpSession session) {

        logger.debug("IndexController.indexjson ......");
        Map result = new HashMap();
// 实例化FurnitureExample  
        FurnitureExample example1 = new FurnitureExample();

// 实例化FurnitureExample内部类Criteria  
        FurnitureExample.Criteria criteria1 = example1.createCriteria();

// 查询所有家具保存到furnitureall1中  
        List furnitureall1 = furnituredao.selectByExample(example1);

// 定义最新家具list  
        List furniturezuixin8 = null;

// 如果所有的家具数量大于了 8 
        if (furnitureall1.size() > 8) {

// 截取furnitureall1中的前8位保存到最新家具list中 
            furniturezuixin8 = furnitureall1.subList(furnitureall1.size() - 9, furnitureall1.size() - 1);

        } else {

// 将所有家具添加到最新家具list中 
            furniturezuixin8 = furnitureall1;

        }

// 将最新家具list保存到request中 
        result.put("furniturezuixin8", furniturezuixin8);

// 实例化AnnounceExample  
        AnnounceExample example2 = new AnnounceExample();

// 实例化AnnounceExample内部类Criteria  
        AnnounceExample.Criteria criteria2 = example2.createCriteria();

// 查询所有公告保存到announceall2中  
        List announceall2 = announcedao.selectByExample(example2);

// 定义最新公告list  
        List announcezuixin8 = null;

// 如果所有的公告数量大于了 8 
        if (announceall2.size() > 8) {

// 截取announceall2中的前8位保存到最新公告list中 
            announcezuixin8 = announceall2.subList(announceall2.size() - 9, announceall2.size() - 1);

        } else {

// 将所有公告添加到最新公告list中 
            announcezuixin8 = announceall2;

        }

// 将最新公告list保存到request中 
        result.put("announcezuixin8", announcezuixin8);

        return result;

    }

    // 上传文件图片等

    public String uploadUtile(MultipartFile file, HttpServletRequest request) throws IOException {

        // 根据当前时间生成时间字符串

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS");

        String res = sdf.format(new Date());

        // uploads文件夹位置

        String tomcatroot = request.getSession().getServletContext().getRealPath("resource/uploads/");
        String localroot = request.getServletContext().getRealPath("").split("out")[0] + "WebRoot/resource/uploads";
        System.out.println(tomcatroot);
        System.out.println(localroot);

        // 原始名称

        String originalFileName = file.getOriginalFilename();

        // 新文件名

        String newFileName = "sliver" + res + originalFileName.substring(originalFileName.lastIndexOf("."));

        // 创建年月文件夹

        Calendar date = Calendar.getInstance();

        File dateDirs = new File(date.get(Calendar.YEAR) + File.separator + (date.get(Calendar.MONTH) + 1));

        // 新文件

        File tomcatFile = new File(tomcatroot + File.separator + dateDirs + File.separator + newFileName);
        File localFile = new File(localroot + File.separator + dateDirs + File.separator + newFileName);

        // 判断目标文件所在目录是否存在

        if (!tomcatFile.getParentFile().exists()) {

            // 如果目标文件所在的目录不存在，则创建父目录

            tomcatFile.getParentFile().mkdirs();

        }
        // 判断目标文件所在目录是否存在

        if (!localFile.getParentFile().exists()) {

            // 如果目标文件所在的目录不存在，则创建父目录

            localFile.getParentFile().mkdirs();

        }

        file.transferTo(tomcatFile);

        String fileUrl = date.get(Calendar.YEAR) + "/" + (date.get(Calendar.MONTH) + 1) + "/" + newFileName;

        return fileUrl;

    }

}

